home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / actlib13.zip / STRINGS.ZIP / STRNCPYB.C < prev    next >
C/C++ Source or Header  |  1993-02-25  |  1KB  |  47 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "strings.h"
  4. #include <stdlib.h>
  5.  
  6.  
  7. /***
  8.  *  Function    : strncpyb
  9.  *
  10.  *  Description :  Like strncpy but truncates trailing blanks and \n,
  11.  *                 skip leading blanks and adds a '\0'.
  12.  *
  13.  *  Decisions   :
  14.  *
  15.  *  Parameters  :  out   char * target
  16.  *               in    char * source
  17.  *                 in    int    length
  18.  *
  19.  *  Return code :  like strncpy
  20.  *
  21.  *  Side-effects:  Pad target string to length with '\0'.
  22.  *
  23.  *  OS/Compiler :  All
  24.  ***/
  25.  
  26. char *strncpyb( char *target, const char *source, int length )
  27.  
  28. { char *lastnonblank = target - 1, *ptr;
  29.   int offs = 0;
  30.  
  31.   while ( *source++ == ' ' ) offs++; source--;
  32.   length -= offs;
  33.  
  34.   for ( ptr = target; (length > 0) && *source; length-- )
  35.       {
  36.         if ( *source != ' ' && *source != '\n' )
  37.            lastnonblank = ptr;
  38.         *ptr++ = *source++;
  39.       }
  40.  
  41.   memset( lastnonblank + 1, '\0', ptr - lastnonblank + length + offs );
  42.  
  43.   return target;
  44. }
  45.  
  46.  
  47.